var listObj = (function ($) {
	var sourceData,method,deleteMethod;
	var pageType = '',imgSrc=""
	var start = 0,Limit = 20;
	var pullDom;
	var pulling = false;
	var perInfo
	var wardID=''
	$.init();
	$.plusReady(function() {
		baseSetup();
	})
	if (!mui.os.plus) {
		baseSetup()
	}
	function deleteOne(rw){
		var pdict = {'id':rw}
		var paramDict = {'method':deleteMethod,'params':JSON.stringify(pdict),'type':'Method'}
		//console.log(JSON.stringify(pdict))
		$.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			if (result.msg != '1') {
				mui.toast('删除失败')
				return
			}
			resetRefresh()
		}, function(errorStr) {
		 	
		});
	}
	
	function getApplyData(){
		if (pulling) {
			pullDom.endPullUpToRefresh()
			return;
		}
		pulling = true
		var stDate = $('#stBtn')[0].innerText
		var endDate = $('#endBtn')[0].innerText
		if (stDate!="" && endDate!="" && stDate > endDate) {
			mui.toast('结束时间不能早于开始时间')
			pullDom.endPullUpToRefresh()
			return;
		}
		//parr 护士Id^开始日期^结束日期
		var pdict = {'start':start,'limit':Limit,'parr':perInfo.loginID+'^'+stDate+'^'+endDate,'nurseid':perInfo.loginID}
		if (pageType != '意愿排班') { //病区id^开始日期^结束日期
			pdict = {'start':start,'limit':Limit,'parr':wardID+'^'+stDate+'^'+endDate,'nurseid':perInfo.loginID}
		}
		var paramDict = {'method':method,'params':JSON.stringify(pdict),'type':'RecQuery'}
		// console.log(JSON.stringify(pdict))
		$.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			var dataArr = result.data
			if (!dataArr) {
				pullDom.endPullUpToRefresh();
				pulling = false
				return;
			}
			
			if (start == 0) {
				sourceData = dataArr;
			}else{
				sourceData = sourceData.concat(dataArr)
			}
			updateUI(dataArr)
			if (dataArr.length < Limit) {
				pullDom.endPullUpToRefresh(true);
			}else{
				pullDom.endPullUpToRefresh();
			}
			start += dataArr.length;
			pulling = false
		}, function(errorStr) {
		 	pullDom.endPullUpToRefresh();
		 	pulling = false
		});
	}
	function updateUI(dataArr){
		var UL = $('#UL')[0];
		if (start == 0) {
			mui('#UL li').each(function (index,element) {
				element.removeEventListener('tap',tableCellTapped)
			})
			UL.innerHTML = ''
		}
		var namekey = 'PerName'
		var createDate = '申请日期'
		if (pageType == '改班申请') {
			namekey = 'ChangeNurseName'
		}
		var oldCount = UL.querySelectorAll('li').length;
		for(var i=0; i<dataArr.length; i++){
			var oneData = dataArr[i]
			var html = getAppHtml(oneData)
			var newLi = document.createElement('li')
			UL.appendChild(newLi)
			newLi.setAttribute('class','mui-table-view-cell')
			newLi.innerHTML = html
			newLi.id = 'li-'+(oldCount+i);
			newLi.addEventListener('tap',tableCellTapped)
		}
	}
	function tableCellTapped(e){
		var index = this.id.split('-')[1]
		var currData = sourceData[index]
		var targetStr = e.target.getAttribute('class')
		var str = 'deleteText mui-icon-trash mui-slider-right mui-btn-red deleteBox'
		if (str.indexOf(targetStr) != -1) {
			mui.confirm('确认删除这条数据吗','删除提示',['取消','确认'],function (e) {
				if (e.index == 1) {
					deleteOne(currData['rw'])
				}
			})
			return
		}
		
		if (currData['Status'] == 'N'&&	perInfo.loginPerID == currData['PerDR']) {
			var webview = plus.webview.create('nurSchAppNew.html', 'nurSchAppNew', {
				background: '#fff',
				statusbar: {
					background: '#fff'
				}
			},{'title':pageType,'data':currData});
			webview.show('fade-in');
			return
		}
		
		var webview = plus.webview.create('nurSchAppInfo.html', 'nurSchAppInfo', {
			background: '#fff',
			statusbar: {
				background: '#fff'
			}
		},{'imgSrc':imgSrc,'title':pageType,'rw':currData['rw']});
		webview.show('fade-in');
		webview.addEventListener('close',resetRefresh)
	}

	//
	function getAppHtml(dict){
		var Status = dict['Status']; //N:保存 S:提交 A:审核 B:驳回
		if (pageType == '换班申请') { //B:驳回 Y:提交 A:审核
			Status = dict['ExchangeStatus']
		}else if (pageType == '改班申请') {
			Status = dict['ChangeStatus']
		}
		var statusIcon = 'nur-status'+Status
		if (Status == 'N') {
			statusIcon = 'nur-statusS'
		}else if (Status == 'S' || Status == 'Y') {
			statusIcon = 'nur-statusR'
		}
		var html = ''
		if (pageType == '意愿排班') {
			html = '<div class="liheader"><img class="headerImg" src="'+imgSrc+'"/>'+
					'<div class="headerText mui-ellipsis">'+dict['PerName']+' '+pageType+'</div>'+
					'<div class="headerTime">'+(dict['CreateDate']==undefined?'':dict['CreateDate'])+'</div></div>'+
				'<div class="statusIcon"><div class="nurfont '+statusIcon+'"></div></div>'+
				'<div class="oneline"><div class="leftText">病区：</div><div class="rightText">'+dict['WardDesc']+'</div></div>'+
				'<div class="oneline"><div class="leftText">日期：</div><div class="rightText">'+dict['WishDate']+'</div></div>'+
				'<div class="oneline"><div class="leftText">班次：</div><div class="rightText">'+dict['PostDesc']+'</div></div>'+
				'<div class="oneline"><div class="leftText">备注：</div><div class="rightText">'+dict['Remark']+'</div></div>';
		}else if (pageType == '改班申请') {
			html = '<div class="liheader"><img class="headerImg" src="'+imgSrc+'"/>'+
					'<div class="headerText mui-ellipsis">'+dict['ChangeNurseName']+' '+pageType+'</div>'+
					'<div class="headerTime">'+dict['CreateDate']+'</div></div>'+
				'<div class="statusIcon"><div class="nurfont '+statusIcon+'"></div></div>'+
				'<div class="oneline"><div class="leftText">改班日期：</div><div class="rightText">'+dict['ChangeSourceDate']+'</div></div>'+
				'<div class="oneline"><div class="leftText">改班班次：</div><div class="rightText">'+dict['ChangeFromPostDesc']+'</div></div>'+
				'<div class="oneline"><div class="leftText">期望班次：</div><div class="rightText">'+dict['ChangeToPostDesc']+'</div></div>';
		}else if (pageType == '换班申请') {
			html = '<div class="liheader"><img class="headerImg" src="'+imgSrc+'"/>'+
					'<div class="headerText mui-ellipsis">'+dict['ExchangeFromNurseName']+' '+pageType+'</div>'+
					'<div class="headerTime">'+dict['CreateDate']+'</div></div>'+
				'<div class="statusIcon"><div class="nurfont '+statusIcon+'"></div></div>'+
				'<div class="oneline"><div class="leftText">申请日期：</div><div class="rightText">'+dict['ExchangeFromArgDate']+'</div></div>'+
				'<div class="oneline"><div class="leftText">申请班次：</div><div class="rightText">'+dict['ExchangeFromArgDesc']+'</div></div>'+
				'<div class="oneline"><div class="leftText">换班日期：</div><div class="rightText">'+dict['ExchangeToArgDate']+'</div></div>'+
				'<div class="oneline"><div class="leftText">换班对象：</div><div class="rightText">'+dict['ExchangeToNurseName']+'</div></div>'+
				'<div class="oneline"><div class="leftText">换班班次：</div><div class="rightText">'+dict['ExchangeToArgDesc']+'</div></div>';
		}
		if (Status == "N" || Status == 'B') { //已保存和已驳回
			html = '<div class="mui-slider-right mui-disabled">'+
				'<div class="mui-btn mui-btn-red"><div class="deleteBox">'+
					'<div class="mui-icon mui-icon-trash"></div><div class="deleteText">删除</div></div>'+
					'<div class="seperateLine"></div></div>'+
				'</div><div class="mui-slider-handle">'+html+'</div>'
		}
		return html
	}

	function baseSetup(){
		$('.mui-scroll-wrapper').scroll({
			bounce: true,
			indicators: false, //是否显示滚动条
		});
		var stBtn = $('#stBtn')[0]
		var endBtn = $('#endBtn')[0]
		var curMonth = getDeviceYearMonthStr();
		stBtn.innerText = curMonth +'-01'
		var currDict = getYMDWithStr(curMonth)
		endBtn.innerText = curMonth +'-' + getMonthMaxDay(currDict['year'], currDict['month'])
		stBtn.addEventListener('tap',dateBtnTapped)
		endBtn.addEventListener('tap',dateBtnTapped)
		var self = plus.webview.currentWebview()
		pageType = self.title
		imgSrc = self.imgSrc
		//console.log(imgSrc)
		perInfo = app.getState()
		wardID=perInfo['loginWardId']
		switch (pageType){
			case '意愿排班':
				method = 'web.INMArgComm:FindWishArrangeList'
				deleteMethod = 'web.INMArgComm:DeleteWishRec'
				break;
			case '换班申请':
				method = 'web.INMArgComm:FindArgExchangeList'
				deleteMethod = 'web.INMArgComm:DeleteExchangeApp'
				break;
			case '改班申请':
				method = 'web.INMArgComm:FindArgChangeList'
				deleteMethod = 'web.INMArgComm:DeleteChangeApp'
				break;
			default:
				break;
		}
		$('.mui-title')[0].innerText = pageType
		var scroll = document.getElementById('scroll');
		pullDom = $(scroll).pullToRefresh({
			up: {
				callback: function() {
					getApplyData()
				}
			}
		});
		getApplyData();
		//新建按钮
		mui('body').on('tap','#addBtn',function(){
			var pageName = "nurSchAppNew"
			var webview = plus.webview.create(pageName+'.html', pageName, {
				background: '#fff',
				statusbar: {
					background: '#fff'
				}
			},{'title':pageType});
			webview.show('fade-in');
		})
	}
	function dateBtnTapped(){
		var _self = this;
		plus.nativeUI.pickDate(function(e) {
			var d = e.date;
			var dateStr = d.getFullYear()+'-'
			if (d.getMonth() < 9) {
				dateStr += '0'
			}
			dateStr += (d.getMonth() + 1)+'-'
			if (d.getDate() < 10) {
				dateStr += '0'
			}
			dateStr += d.getDate()
			_self.innerText = dateStr;
			resetRefresh();
		}, function(e) {}, {});
	}
	
	var resetRefresh = function (){
		start = 0;
		sourceData = []
		mui('#UL li').each(function (index,element) {
			element.removeEventListener('tap',tableCellTapped)
		})
		 $('#UL')[0].innerHTML = '';
		 $('#wrapper').scroll().scrollTo(0,0,100);
		if (pullDom) {
			pullDom.refresh(true)
		}
		getApplyData();
	}
	var exportObj = {}
	exportObj.resetRefresh = resetRefresh
	return exportObj
})(mui)

function detailChanged(){
	listObj.resetRefresh();
}

/* 意愿排班
	{"WardDR":"29","WardDesc":"内分泌科护理单元","PerDR":"698","PerName":"刘亭婷","WishDate":"2022-04-25","PostDR":"69","PostDesc":"P","Remark":"备注",
	"Status":"S","StatusDesc":"已提交","Auditor":"","AuditorName":"","AuditDate":"","rw":"20","ArgTime":"07:30-11:30;14:0018:00"}

换班申请
	{"ExchangeWard":"29","ExchangeWardDesc":"内分泌科护理单元","ExchangeFromNurse":"698","ExchangeFromNurseName":"刘亭婷","ExchangeFromArg":"5515",
	"ExchangeFromArgDesc":"A","ExchangeFromArgDate":"2022-02-15","ExchangeToNurse":"681","ExchangeToNurseName":"张乐晗","ExchangeToArg":"5453",
	"ExchangeToArgDesc":"P","ExchangeToArgDate":"2022-02-15","ExchangeStatus":"Y","ExchangeStatusDesc":"提交","CreateDate":"2022-04-26",
	"CreateTime":"10:47:51","Auditor":"","AuditorName":"","AuditDate":"","AuditTime":"","AuditOpinion":"","rw":"13"}
	
改班申请
	{"ChangeWard":"29","ChangeWardDesc":"内分泌科护理单元","ChangeNurse":"698","ChangeNurseName":"刘亭婷","ChangeSource":"5312",
	"ChangeSourceDate":"2022-02-09","ChangeFromPost":"68","ChangeFromPostDesc":"A","ChangeToPost":"69","ChangeToPostDesc":"P","ChangeStatus":"Y",
	"ChangeStatusDesc":"提交","CreateDate":"2022-04-25","CreateTime":"17:53:42","Auditor":"","AuditorName":"","AuditDate":"","AuditTime":"",
	"AuditOpinion":"","rw":"7"}

*/